<?php
/**
 * 场外交易卖出.
 * User: madong
 * Date: 2018/10/2
 * Time: 7:21 PM
 */

namespace app\api\controller\jiaoyi;

use app\api\model\JiaoyiUinfo;
use app\api\model\User;
use app\api\model\Jiaoyi;
use app\api\model\Adeth;
use think\Controller;
use think\Request;
use think\Config;
use think\Db;

class Changwaifabu extends Controller
{

    private $gas = 0.0005;//手续费

    private $xiao = 0.01;//卖出最小限制

    private $sellvalue = 1;//最小出售数量

    private $buyvalue = 1000;//最大购买数量

    /*
     * 发布卖单处理
     */
    public function Sell_order_dispose()
    {



        $jiaoyiobj = new Jiaoyi();

        $Main_User = new User();

        $U_info = new JiaoyiUinfo();

        $res = Request::instance();

        //echo json_encode(['zt' => 0, 'msg' => $res->param('bizhong')]);die();

        $Jiaoyi_Usur_info = $U_info->where('uname', $res->param('uname'))->find();//交易所指定用户全部信息

        $Main_User_Info = $Main_User->where('uname', $res->param('uname'))->find();//系统主用户表

        $ifv_Order1 = Db::name('jiaoyi')->where('uname',$res->param('uname'))->where('bizhong',$res->param('bizhong'))->where('type',1)->find();

        $ifv_Order0 = Db::name('jiaoyi')->where('uname',$res->param('uname'))->where('bizhong',$res->param('bizhong'))->where('type',0)->find();





        if (!$res->isPost()){echo json_encode(['zt'=>2,'msg'=>'Error']);die();}


        if (!$res->param('xiao') && !$res->param('da')){echo json_encode(['zt'=>0,'msg'=>'最大或最小不能为空']);die();};

        if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/',$res->param('xiao')) && !preg_match('/^[0-9]+(.[0-9]{1,2})?$/',$res->param('da'))){echo json_encode(['zt'=>0,'msg'=>'请输入整数']);die();}

        if ($res->param('order_type') == 1){//为1的时候为卖出状态

            if ($ifv_Order1){echo json_encode(['zt'=>0,'msg'=>'同一类型的广告只能发布一条']);die();};

            if ($res->param('xiao') < $this->xiao){echo json_encode(['zt'=>0,'msg'=>'最小数量不能小于'.$this->xiao]);die();}

            if ($res->param('da') > $Jiaoyi_Usur_info[strtolower($res->param('bizhong'))]){echo json_encode(['zt'=>0,'msg'=>'最大不能超过账户余额'.$Jiaoyi_Usur_info[strtolower($res->param('bizhong'))]]);die();}

            if ($Jiaoyi_Usur_info[strtolower($res->param('bizhong'))] <= 0){echo json_encode(['zt'=>0,'msg'=>'您当前'.$res->param('bizhong').'数量为'.$Jiaoyi_Usur_info[strtolower($res->param('bizhong'))]]);die();}

            if (!$Jiaoyi_Usur_info['bankname'] || !$Jiaoyi_Usur_info['zhifubaoimg'] || !$Jiaoyi_Usur_info['weixinaccount']){echo json_encode(['zt'=>0,'msg'=>'请先完善收款信息']);die();}

            Db::startTrans();

            try {

                Db::name('jiaoyi')->insert([

                    'uname' => $res->param('uname'),//发布人账号（卖）

                    'truename' => $Jiaoyi_Usur_info['truename'],//在交易所用户获取用户的真实姓名

                    'touxiang' => $Main_User_Info['headimg'],//在系统主用户表获取最新的用户头像

                    'uid' => $Jiaoyi_Usur_info['uid'],//在交易所获取当前用户ID

                    'value' => $res->param('value'),//卖出的数量（已废弃）

                    'sysl' => $res->param('value'),//卖出的订单剩余数量（已废弃）

                    'bizhong' => strtolower($res->param('bizhong')),//发布卖出的币种（转换一下小写）

                    'jiage' => $res->param('jiage'),//货币价格（只是百分比）

                    'xiao' => $res->param('xiao'),//最小购买数量

                    'da' => $res->param('da'),//最大购买数量

                    'yinlian' => $Jiaoyi_Usur_info['bankname'],//在交易所用户表获取银行卡号

                    'khh' => $Jiaoyi_Usur_info['bankkaihu'],//在交易所用户表获取银行名称

                    'zfb' => $Jiaoyi_Usur_info['zhifubaoaccount'],//在交易所用户表获取支付宝账号

                    'zfbpic' => $Jiaoyi_Usur_info['zhifubaoimg'],//在交易所用户表获取支付宝收款码

                    'weixinpic' => $Jiaoyi_Usur_info['weixinimg'],//在交易所用户表获取微信收款码

                    'weixin' => $Jiaoyi_Usur_info['weixinaccount'],//在交易所用户表获取微信账号（意义不大）

                    'type' => $res->param('order_type'),//订单类型 1 卖出 0 买入

                    'typedescribe' => '卖出',

                    'jiaoyics' => 0,//当前广告的交易数量（成功的订单）

                    'xinren' => 0,//当前订单的信任数量（成功订单的数量）

                    'haoping' => 0,//当前的好评（暂时没作用）

                    'ordernumber' => strtoupper($res->param('bizhong')) . '-' . strtoupper(md5($res->param('uname') . time())),//当前订单编号

                    'note' => strip_tags($res->param('note')),//当前订单说明

                    'time' => time()//当前订单创建时间

                ]);

                Db::commit();

                echo json_encode(['zt' => 1, 'msg' => '卖出广告发布成功']);

            } catch (\Exception $e) {

                echo json_encode(['zt' => 0, 'msg' => '广告发布失败,稍后重试!']);

                Db::rollback();
            }

        }elseif ($res->param('order_type') == 0){//为0的时候为买入状态----------------------------------------------------------------------

            if ($ifv_Order0){echo json_encode(['zt'=>0,'msg'=>'同一类型的广告只能发布一条']);die();};

            if ($res->param('xiao') < $this->xiao){echo json_encode(['zt'=>0,'msg'=>'最小数量不能小于'.$this->xiao]);die();}

            if ($res->param('da') > $this->buyvalue){echo json_encode(['zt'=>0,'msg'=>'最大不能超过'.$this->buyvalue]);die();}

            Db::startTrans();

            try {

                Db::name('jiaoyi')->insert([

                    'uname' => $res->param('uname'),//当前用户名（买入）

                    'truename' => $Jiaoyi_Usur_info['truename'],//在交易所用户表获取真实姓名

                    'touxiang' => $Main_User_Info['headimg'],//在系统主表获取用户头像

                    'uid' => $Jiaoyi_Usur_info['uid'],//交易所用户表获取用户ID

                    'value' => $res->param('value'),//买入数量（暂时不知道有没有用）

                    'sysl' => $res->param('value'),//还差多少求购满（暂时不知道有没有用）

                    'bizhong' => strtolower($res->param('bizhong')),//求购的币种

                    'jiage' => $res->param('jiage'),//求购的价格

                    'xiao' => $res->param('xiao'),//最少求购数量

                    'da' => $res->param('da'),//最大求购数量

                    'yinlian' => $Jiaoyi_Usur_info['bankname'],//交易所用户表获取银行卡号

                    'khh' => $Jiaoyi_Usur_info['bankkaihu'],//交易所用户表获取银行名称

                    'zfb' => $Jiaoyi_Usur_info['zhifubaoaccount'],//交易所用户表获取支付宝账号

                    'zfbpic' => $Jiaoyi_Usur_info['zhifubaoimg'],//交易所获取用户表获取支付宝收款码

                    'weixinpic' => $Jiaoyi_Usur_info['weixinimg'],//交易所获取用户表微信收款码

                    'weixin' => $Jiaoyi_Usur_info['weixinaccount'],//交易所获取用户表微信账号（意义不大）

                    'type' => $res->param('order_type'),//类型0

                    'typedescribe' => '买入',

                    'jiaoyics' => 0,//交易次数

                    'xinren' => 0,//信任次数

                    'haoping' => 0,//好评

                    'ordernumber' => strtoupper($res->param('bizhong')) . '-' . strtoupper(md5($res->param('uname') . time())),//当前订单号

                    'note' => strip_tags($res->param('note')),//当前订单备注

                    'time' => time()//当前订单创建时间

                ]);

                Db::commit();

                echo json_encode(['zt' => 1, 'msg' => '求购广告发布成功']);

            } catch (\Exception $e) {

                echo json_encode(['zt' => 0, 'msg' => '求购广告发布失败,稍后重试!']);

                Db::rollback();
            }

        }else{

            echo json_encode(['zt'=>0,'msg'=>'不要恶意请求']);

        }

    }

    /*
     * 获取当前用户交易所的余额
     *
     * 参数
     *
     * @uname  当前用户名
     *
     * @type  货币类型
     *
     * 返回
     *
     * 直接返回余额
     *
     */
    protected function Get_jiaoyi_balance($uname = '', $type = '')
    {

        $jiaoyiinfoobj = new JiaoyiUinfo();

        $balance = $jiaoyiinfoobj->where('uname', $uname)->value($type);

        return $balance;

    }
    //获取排名
    public function Get_user_paiming(){

        $res = Request::instance();

        if ($res->isPost()){

            $zuida = Db::name('jiaoyi')->order('jiage desc')->value('jiage');//获取最大值

            if ($res->param('jiage') > $zuida){

                echo json_encode(['zt'=>1,'msg'=>1]);

            }elseif ($res->param('jiage') == $zuida){

                echo json_encode(['zt'=>1,'msg'=>1-2]);

            }elseif ($res->param('jiage') < $zuida){

                $pai = Db::name('jiaoyi')->where('jiage','>=',$res->param('jiage'))->where('jiage','<=',$zuida)->select();

                echo json_encode(['zt'=>1,'msg'=>count($pai)+1]);

            }else{

                echo json_encode(['zt'=>0,'msg'=>'排名查询失败']);

            }

        }else{

            echo json_encode(['zt'=>2,'msg'=>'error']);

        }

    }

}